package com.base.system.config.security;

import com.base.system.enums.UserState;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

@Data
@ToString(exclude = "password")
public class AuthUser implements UserDetails {

    private static final long serialVersionUID = 2985772429693169693L;
    private Long id;
    /**
     * 昵称
     */
    private String nickName;

    /**
     * 真实名称
     */
    private String realName;

    /**
     * 账号
     */
    private String userAcount;

    /**
     * 性别
     */
    private String sex;

    private String idNumber;

    private String phone;

    private String email;
    /**
     * 年龄
     */
    private Integer age;

    /**
     * 头像
     */
    private String actor;

    /**
     * 用户状态
     */
    private UserState state;
    @Getter(AccessLevel.NONE)
    @JsonIgnore
    private String password;

    private Set<String> roles = new HashSet<>();

    private Set<String> pres = new HashSet<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new LinkedList<>();
        for (String permission : pres) authorities.add(new SimpleGrantedAuthority(permission));

        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return userAcount;
    }

    @Override
    public boolean isAccountNonExpired() {
        return !UserState.expired.equals(state);
    }

    @Override
    public boolean isAccountNonLocked() {
        return !UserState.locked.equals(state);
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return UserState.normal.equals(state);
    }
}
